﻿using System;
using System.Collections.Generic;
using System.IO;

namespace Glossary
{
    [Obsolete]
    static public class Output
    {

        //public List<Word> WordList { get; set; }
        //public int CurrentLang { get; set; }

        /// <summary>
        /// Reads a file, throws away the first line. And parses each line after as its format Foreign;Swedish creating a list containing words for each line.
        /// </summary>
        /// <param name="path">Path to which file</param>
        /// <returns>The List of words</returns>
        static public List<Word> ReadFromFile(string path)
        {
            if (!File.Exists(path))
                throw new FileNotFoundException("The directory you are saving to does not exist.");
            using (StreamReader reader = new StreamReader(path))
            {
                List<Word> outPut = new List<Word>();
                string line = reader.ReadLine();

                if (line == null)
                    throw new NullReferenceException("File is entirely empty.");

                while ((line = reader.ReadLine()) != null)
                {
                    String[] str = line.Split(';');
                    if (str.Length >= 2)
                        outPut.Add(new Word(str[0], str[1]));
                }

                if (outPut.Count == 0)
                    throw new FormatException("File does not contain any words.");
                return outPut;
            }
        }

        /// <summary>
        /// For teacher when reading the uncorrected exam before correcting it.
        /// </summary>
        /// <param name="path"></param>
        /// <param name="firstLine"></param>
        /// <returns></returns>
        static public List<Word> ReadUncorrectedExam(string path, ref string[] firstLine)
        {
            if (!File.Exists(path))
                throw new FileNotFoundException("The directory you are saving to does not exist.");
            using (StreamReader reader = new StreamReader(path))
            {
                List<Word> outPut = new List<Word>();
                string line = reader.ReadLine();
                if (line == null || line == "")
                    throw new NullReferenceException("File is entirely empty.");

                firstLine = line.Split(';');

                while ((line = reader.ReadLine()) != null)
                {
                    String[] str = line.Split(';');
                    if (str.Length >= 2)
                        outPut.Add(new Word(str[0], str[1], str[2]));
                }

                if (outPut.Count == 0)
                    throw new FormatException("File does not contain any words.");
                return outPut;
            }

        }

        /// <summary>
        /// For student when reading their now corrected exam.
        /// </summary>
        /// <param name="p"></param>
        /// <param name="referenceString"></param>
        public static List<Word> ReadCorrectedExam(string path, ref string[] firstLine)
        {
            if (!File.Exists(path))
                throw new FileNotFoundException("The directory you are saving to does not exist.");
            Console.WriteLine(path);
            using (StreamReader stmrdr = new StreamReader(path))
            {
                List<Word> outPut = new List<Word>();
                string line = stmrdr.ReadLine();
                if (line == null || line == "")
                    throw new NullReferenceException("File is entirely empty.");

                firstLine = line.Split(';');

                while ((line = stmrdr.ReadLine()) != null)
                {
                    String[] str = line.Split(';');
                    foreach (string item in str)
                    {
                        Console.WriteLine(item);
                    }
                    if (str.Length != 3) { }
                    bool corrected = (str[3] == "True");
                    outPut.Add(new Word(str[0], str[1], str[2], corrected));

                }

                if (outPut.Count == 0)
                    throw new FormatException("File does not contain any words.");
                return outPut;
            }
        }

        /// <summary>
        /// Gets the language enum from a file.
        /// </summary>
        /// <param name="path">Path to the file</param>
        /// <returns>Language of the file.</returns>
        static public Languages GetFileLanguage(string path)
        {
            Languages lang;
            if (!File.Exists(path))
                throw new FileNotFoundException("The file does not exist.");
            using (StreamReader stmrdr = new StreamReader(path))
            {
                List<Word> outPut = new List<Word>();
                string line = stmrdr.ReadLine();

                if (line == null)
                    throw new NullReferenceException("File is entirely empty.");
                string[] str = line.Split(';');

                try
                {
                    lang = (Languages)int.Parse(str[0]);
                }
                catch (Exception)
                {
                    throw new FormatException("Files language identifier was wrong.");
                }
            }
            return lang;
        }


        /// <summary>
        /// Used by student to get and exam file.
        /// </summary>
        /// <param name="path"></param>
        /// <param name="currentLang"></param>
        /// <returns></returns>
        static public List<Word> GetExam(string path, out Languages currentLang)
        {
            string[] filename = path.Split('\\');
            filename = filename[filename.Length - 1].Split('_');
            if (!filename[0].Equals("exam"))
                throw new WrongFileException("File is not an exam file.");

            currentLang = GetFileLanguage(path);
            return Output.ReadFromFile(path);
        }
    }
}
